# 配置管理
import logging
import os

from openai import AsyncAzureOpenAI, AzureOpenAI
from pymongo import MongoClient
from sqlalchemy import create_engine, text

# 配置日志
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# ===========================================================================================================================
# 连接 MongoDB
try:
    db_host = "mongodb://simon:BWOYO0R7oDt40y0shH6t@47.107.47.84:27017/"
    mongo_engine = MongoClient(db_host)
    # 测试连接
    mongo_engine.admin.command('ping')
    logger.info("成功连接到 MongoDB")
except Exception as e:
    logger.error(f"连接 MongoDB 失败: {e}")
# ===========================================================================================================================
response_body = {
    'code': 200,
    'msg': 'success',
    'data': {}
}

# ===========================================================================================================================
# azure api 配置

API_KEY = "1KHm8zFftbDwil33cdFOTvBZo3DEaaPQujH6ijY5AmhgeBQQ0dn2JQQJ99BGACMsfrFXJ3w3AAABACOGqalv"
AZURE_ENDPOINT = "https://starit-openai.openai.azure.com/"
API_VERSION = "2024-12-01-preview"
client_azure = AzureOpenAI(
    api_key=API_KEY,
    api_version=API_VERSION,
    azure_endpoint=AZURE_ENDPOINT,
)
# ===========================================================================================================================
# MySQL 连接字符串格式：
# mysql+pymysql://用户名:密码@主机:端口/数据库名
try:
    mysql_engine = create_engine(
        "mysql+pymysql://root:BWOYO0R7oDt40y0shH6t@47.107.47.84:53306/tools-simon"
    )
    # 测试连接
    with mysql_engine.connect() as conn:
        result = conn.execute(text("show databases;")).mappings().all()
        print(result)
    logger.info("成功连接到 tools-simon 数据库")
except Exception as e:
    logger.error(f"连接 tools-simon 数据库失败: {e}")
